package org.springframework.remoting.jsonrpc.test;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.jabsorb.JSONSerializer;
import org.jabsorb.serializer.UnmarshallException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.remoting.jsonrpc.ClassSerializer;
import org.springframework.remoting.jsonrpc.ObjectSerializer;
import org.springframework.remoting.support.RemoteInvocation;

public class UnMarshallTestCase extends TestCase {

	ApplicationContext ctx;
	
	public UnMarshallTestCase() {
		super();
	}

	public UnMarshallTestCase(String arg0) {
		super(arg0);
	}
	
    public void testUnMarshall1()
    {
    	try {
    	    JSONSerializer serializer = (JSONSerializer)ctx.getBean("serializer");
		    serializer.registerDefaultSerializers();
		    serializer.registerSerializer(new ClassSerializer());	
		    serializer.registerSerializer(new ObjectSerializer());

    		String json = serializer.toJSON(new String("hello"));
    		System.out.println(json);
			Object obj = serializer.fromJSON("{\"arguments\":[\"name\"],\"attributes\":null,\"javaClass\":\"org.springframework.remoting.support.RemoteInvocation\",\"parameterTypes\":[\"java.lang.String\"],\"methodName\":\"getAccounts\"}");
			Assert.assertTrue(obj!=null);
		} catch (UnmarshallException e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}
		  catch (Exception e)
		  {
			  e.printStackTrace();
		  }
    }
    public void testUnMarshall2()
    {
    	try {
    	JSONSerializer serializer = (JSONSerializer)ctx.getBean("serializer");
    	serializer.registerDefaultSerializers();
    	serializer.registerSerializer(new ClassSerializer());
    	serializer.registerSerializer(new ObjectSerializer());

	    //String s ="{\"arguments\":[{\"javaClass\":\"org.springframework.remoting.jsonrpc.test.Account","name":"account1","id":0}],"attributes":null,"javaClass":"org.springframework.remoting.support.RemoteInvocation","parameterTypes":["org.springframework.remoting.jsonrpc.test.Account"],"methodName":"insertAccount"}
			Object obj = serializer.fromJSON("{\"arguments\":[{\"javaClass\":\"org.springframework.remoting.jsonrpc.test.Account\",\"name\":\"account\",\"id\":0}],\"attributes\":null,\"javaClass\":\"org.springframework.remoting.support.RemoteInvocation\",\"parameterTypes\":[\"org.springframework.remoting.jsonrpc.test.Account\"],\"methodName\":\"insertAccount\"}");
			RemoteInvocation ri = (RemoteInvocation)obj;
			Assert.assertTrue(ri!=null);
		} catch (UnmarshallException e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Assert.assertTrue(false);
			
		}
    }
    
    public void testUnMarshall3()
    {
    	JSONSerializer serializer = (JSONSerializer)ctx.getBean("serializer");
    	try {
        	serializer.registerDefaultSerializers();
        	serializer.registerSerializer(new ClassSerializer());
        	serializer.registerSerializer(new ObjectSerializer());
			Object obj = serializer.fromJSON("{\"arguments\":[4],\"attributes\":null,\"javaClass\":\"org.springframework.remoting.support.RemoteInvocation\",\"parameterTypes\":[\"java.lang.Integer\"],\"methodName\":\"getAccountArray\"}");
			RemoteInvocation ri = (RemoteInvocation)obj;
			Assert.assertTrue(ri!=null);
		} catch (UnmarshallException e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
    }        
    
	protected void setUp() throws Exception {
		super.setUp();
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
		        new String[] {"/org/springframework/remoting/jsonrpc/test/accountTest.xml"});
		this.ctx = (ApplicationContext) ctx;
	}
	

}
